clear
set more off

cap log close
log using "$logdir/analysis/common_bad_situations.log", replace

// load the program
do "$adir/get_bad_dummies"




use $statadir/soi_cdw_taxsim_2019 if nonfiler==0, clear
*****************************************
*										*
*		Compute the bad dummies			*
*										*
*****************************************
get_bad_dummies, thr(500) cumul_thr(1000) cred_thr(100)



*************************************
*									*
*		Collapse and format			*
*									*
*************************************

*************************************
*	Share with ONLY this condition	*
*************************************
local badlist sched_c deps itemizer fil_stat rents_royal txbl_pens depcare cg pt t_unkn ///
	eitc_deps aboveline qbi_ded educ res_energ sched_f noncap_gains alimony early_roth wages int qual_divs
	
foreach k in `badlist' {
	gen only_`k' = bad_`k'
	foreach kk in `badlist' {
		if "`k'"~="`kk'" {
			qui replace only_`k' = 0 if bad_`kk' == 1
		}
	}
}
gegen tot_bad = rowtotal(bad_*)
qui replace tot_bad = tot_bad - bad_any
gen exactly_one = tot_bad == 1
qui sum exactly_one [aw=soi_wgt]
local exactly_one : di %6.3f r(mean)


gcollapse bad_* only_* [aw=soi_wgt], fast
gen k = 1
reshape long bad_ only_, i(k) j(situation) string

gen any = situation == "any"

gsort +any -bad
tostring bad only, format(%6.3f) force replace

tempfile touse
qui save `touse', replace

local N = _N-1







*************************************
*									*
*			Write to table			*
*									*
*************************************


local sched_cstr "Schedule C income does not match 1099-MISC NEC"
local depsstr "Dependent mismatch from 2018 to 2019"
local itemizerstr "Itemized deductions in 2019"
local fil_statstr "Change in filing status from 2018 to 2019"
local rents_royalstr "Sched E rents/royalties (except from K-1)"
local txbl_pensstr "Taxable pension/IRA income does not match 1099-R"
local depcarestr "Child/dependent care credit"
local cgstr "Capital gains income does not match 1099-B"
local ptstr "S corp/partnership income does not match K-1"
local t_unknstr "1099-R with taxable amount not determined"
local eitc_depsstr "EITC dependent mismatch from 2018 to 2019"
local abovelinestr "Certain above-the-line deductions"
local qbi_dedstr "Section 199A deduction complications"
local educstr "Lifetime learning credit"
local res_energstr "Residential energy credits"
local sched_fstr "Schedule F income"
local noncap_gainsstr "Noncapital gains (Form 4797)"
local alimonystr "Received or paid alimony"
local early_rothstr "Early distribution from Roth IRA"
local wagesstr "Wages do not match W-2 wages"
local intstr "Interest does not match 1099-INT" 
local qual_divsstr "Dividends do not match 1099-DIV"

foreach file in bad_sits bad_sits_paper {
	cap file close myfile
	file open myfile using "$outdir/tables/`file'.tex", write replace

	*************
	*	Header	*
	*************
	file write myfile " Situation & Share with this & Share with \textit{only} this \\  "
	file write myfile "			&	failure situation  & failure situation \\ " 
	file write myfile " & (1) & (2) \\ \hline "
	

	
	if "`file'" == "bad_sits" {
		local numrows = 6
	}
	else {
		local numrows = `N'
	}
	
	forval i = 1/`numrows' {
		use `touse', clear
		qui keep if _n == `i'
		
		local situation = situation
		file write myfile " ``situation'str' & `=bad_[1]' & `=only_[1]'  \\ "
	}

	file write myfile " \hline \addlinespace[8pt] "
	use `touse', clear
	qui keep if _n == `numrows'+1
	file write myfile " Any failure situation & `=bad_[1]' \\ "
	file write myfile " Precisely one failure situation & `exactly_one' \\ "




	cap file close myfile
}


cap log close


